package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.constant.MessageConstant;
import com.atguigu.entity.PageResult;
import com.atguigu.entity.QueryPageBean;
import com.atguigu.entity.Result;
import com.atguigu.pojo.TravelGroup;
import com.atguigu.service.TravelGroupService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author LEO
 * @Date 2021年12月17日 23:26
 */
// 跟团游
@RestController
@RequestMapping("pages/travelGroup")
public class TravelGroupController {


   @Reference
    private TravelGroupService travelGroupService;


   // 添加跟团游套餐
   @RequestMapping("/add")
   public Result add(@RequestBody TravelGroup travelGroup, Integer[] travelItemIds){
       travelGroupService.add(travelGroup,travelItemIds);
       return new Result(true, MessageConstant.ADD_TRAVELGROUP_SUCCESS);
   }


   //跟团游条件查询、分页
    // 传递当前页，每页显示的记录数，查询条件
    // 响应PageResult，封装总记录数，结果集
   @RequestMapping("/findPage")
   public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
       PageResult pageResult =  travelGroupService.findPage(
               queryPageBean.getCurrentPage(),
               queryPageBean.getPageSize(),
               queryPageBean.getQueryString());
       return pageResult;
   }


   //根据id查询跟团游
    @GetMapping("/findById")
    public Result getById(Integer id){
        try {
           TravelGroup travelGroup=  travelGroupService.getById(id);
            return new Result(true, MessageConstant.QUERY_TRAVELGROUP_SUCCESS,travelGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_TRAVELGROUP_FAIL);
        }
    }

    @GetMapping("findTravelItemIdByTravelgroupId")
    public Result findTravelItemIdByTravelgroupId(Integer id){
        try {
            List<Integer> travelitemId= travelGroupService.findTravelItemIdByTravelgroupId(id);
            return new Result(true,"根据跟游团查询自由行成功",travelitemId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"根据跟游团查询自由行失败");
        }
    }

    // 编辑跟团游（返回 public Result(boolean flag, String message)）
    @RequestMapping("/edit")
    public Result edit(Integer[] travelItemIds,@RequestBody TravelGroup travelGroup ){
        travelGroupService.edit(travelItemIds,travelGroup);
        return new Result(true,MessageConstant.EDIT_TRAVELGROUP_SUCCESS);
    }


    //删除跟团游
    @GetMapping("/delete")
    public Result delete(Integer id){
        try {
            travelGroupService.deleteById(id);
            return new Result(true,MessageConstant.DELETE_TRAVELGROUP_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_TRAVELGROUP_FAIL);
        }
    }


    @GetMapping("/findAll")
    public Result findAll(){

           List<TravelGroup>  list=  travelGroupService.findAll();
        if(list != null && list.size() > 0){
            return new Result(true,MessageConstant.QUERY_TRAVELGROUP_SUCCESS,list);
        }else {
            return new Result(false,MessageConstant.QUERY_TRAVELGROUP_FAIL);
        }
    }

}
